package org.linlinjava.litemall.admin.web.distribution;

import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.WithdrawEnum;
import org.linlinjava.litemall.db.dao.DisWithdrawRecordMapper;
import org.linlinjava.litemall.db.domain.DisMemberAmount;
import org.linlinjava.litemall.db.domain.DisMemberInfo;
import org.linlinjava.litemall.db.domain.DisWithdrawRecord;
import org.linlinjava.litemall.db.service.distribution.DisMemberAmountService;
import org.linlinjava.litemall.db.service.distribution.DisMemberInfoService;
import org.linlinjava.litemall.db.service.distribution.DisWithdrawRecordService;
import org.linlinjava.litemall.db.util.DateTools;
import org.linlinjava.litemall.db.vo.DisWithdrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提现记录控制器
 *
 * @author xiaojiang
 * @Date 2018-05-30 16:55:08
 */
@RestController
@RequestMapping("/admin/disWithdrawRecord")
public class DisWithdrawRecordController extends BaseController {


    @Autowired
    DisMemberInfoService disMemberInfoService;

    @Autowired
    DisMemberAmountService memberAmountService;

    @Autowired
    DisWithdrawRecordService disWithdrawRecordService;

    @Autowired
    DisWithdrawRecordMapper disWithdrawRecordMapper;




    /**
     * 获取提现记录列表
     */
    @GetMapping(value = "/list")
        @ResponseBody
        public Object list(@LoginAdmin Integer adminId, String userId,
                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                           String sort) {
        if (adminId == null) {
            return ResponseUtil.fail401();
        }
        List<DisWithdrawRecord> list = disWithdrawRecordService.selectList(userId,page,limit);
        list.forEach(record->{
            record.setWithdrawStatusName(WithdrawEnum.getNameByCode(record.getWithdrawStatus()));
        });
        int total = disWithdrawRecordService.countSeletive(userId,page,limit,sort);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);
    }

    /**
     * 新增提现记录
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Object add(DisWithdrawVo withdrawVo) {
       /* DisMemberInfo memberInfo=disMemberInfoService.selectListByUserId(withdrawVo.getUserId());
        if(memberInfo==null){
            throw  new Exception(1000);
        }
        String acc= Jwt.unsign(withdrawVo.getSecret(),secret,String.class);
        if(account.equals(acc)) {
            disWithdrawRecordService.withdrawMoney(withdrawVo.getUserId(),withdrawVo.getAmount(),withdrawVo.getDisProType());
        }else {
            throw new BussinessException(BizExceptionEnum.ILLEGAL_INFO);
        }*/
      return null;
    }

    /**
     * 删除提现记录
     */
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public Object delete() {

        return null;
    }


    /**
     * 修改提现记录
     */
    @PutMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestBody DisWithdrawRecord record) {
        try {
            String now = DateTools.getCurrentSysData(DateTools.FULLTIME_FORMAT);
            DisMemberAmount acount =  memberAmountService.getByuserId(record.getDisUserId());
            if(acount.getFrozenAmount()!=null){
                acount.setFrozenAmount(acount.getFrozenAmount().subtract(record.getTotalAmount()));
            }
            //放款操作  审批通过时更改冷冻金和提现总额
            if(DisWithdrawRecord.STATUS_Y.equals(record.getDisProType())){
                if(acount.getWithdrawAmount()!=null){
                    acount.setWithdrawAmount(acount.getWithdrawAmount().add(record.getTotalAmount()));
                }else{
                    acount.setWithdrawAmount(record.getTotalAmount());
                }
            }else{
                //审批拒绝  冷冻金减少   可用资金增加
                if(acount.getAvaibleAmount()!=null){
                    acount.setAvaibleAmount(acount.getAvaibleAmount().add(record.getTotalAmount()));
                }else{
                    acount.setAvaibleAmount(record.getTotalAmount());
                }
            }
            acount.setUpdateTime(now);
            memberAmountService.update(acount);


            record.setHandleTime(now);
            disWithdrawRecordService.update(record);
        } catch (Exception e) {
            return ResponseUtil.fail(500, "更新失败，" + e.getMessage());
        }
        return ResponseUtil.ok(record);
    }
    @RequestMapping(value = "/audit")
    @ResponseBody
    public Object audit(Integer id,String type) {
      //  disWithdrawRecordService.dealWithdrawl(id,type);
        return null;
    }

    /**
     * 提现记录详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
